﻿using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Demo.UserService.Services;
using Demo.OrderService.Services;
using Demo.OrderService.Services.Dtos;
using Demo.UserService.Services.Dtos;
using Mapster;
using Servicecomb.Saga.Omega.Abstractions.Transaction;
using Newtonsoft.Json;

namespace Demo.WebSite.Controllers
{
    /// <summary>
    /// 同时添加订单和商品控制器
    /// </summary>
    [ApiController]
    [Route("AddOrderProduct")]
    public class AddOrderProductController : AbpController
    {
        /*
            不要使用try catch处理异常
            必须是同步方法才能触发事务
         */

        public IUserService UserService { get; set; }
        public IOrderService OrderService { get; set; }

        /// <summary>
        /// 同时添加订单和商品方法
        /// </summary>
        /// <returns></returns>
        [HttpPost("AddOrderProduct")]
        [SagaStart] //开启分布式事务
        public string AddOrderProduct(ShopOrderDto orderProductDto)
        {

            // 1、创建订单
            var productDto = OrderService.CreateOrderAsync(orderProductDto).Result;

            // 2、添加会员
            UserInfoDto userInfoDto = orderProductDto.Adapt<UserInfoDto>();

            userInfoDto.Salt = "";
            userInfoDto.Status = "1";
            userInfoDto.RealName = "111";
            userInfoDto.Sex = "1";
            userInfoDto.Avatar = "1";

            userInfoDto.Status = "1";
            userInfoDto.IsDeleted = "0";
            userInfoDto.Ip = "127.0.0.1";
            userInfoDto.LoginDate = DateTime.Now;
            userInfoDto.PasswordChangeDate = DateTime.Now;
            userInfoDto.Creator = "1";
            userInfoDto.CreateDate = DateTime.Now;
            userInfoDto.UpdateTime = DateTime.Now;
            userInfoDto.Remark = "!111111111";
            userInfoDto.IsCertification = 1;
            userInfoDto.Token = "111";
            userInfoDto.Modifier = "admin";
            userInfoDto.UserName = orderProductDto.Account;
            userInfoDto.UserType = "1";

            UserInfoDto orderDto = UserService.AddUserAsync(userInfoDto).Result;

            return "同时添加成功"; 
        } 
    } 
}
